//
//  LuckyApi.m
//  SMTools
//
//  Created by fengsh on 2018/12/29.
//  Copyright © 2018年 fengsh. All rights reserved.
//

#import "LuckyApi.h"
#import <AFNetworking/AFNetworking.h>

@interface LuckyApi()
@property (nonatomic, strong) AFHTTPSessionManager              *jsonManager;
@property (nonatomic, strong) AFHTTPSessionManager              *httpManager;
@end

@implementation LuckyApi

+ (instancetype)api
{
    static LuckyApi       *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[LuckyApi alloc]init];
        [instance defaultmanager];
        instance.urlbase = @"http://139.224.130.230:8095";
    });
    return instance;
}

- (void)defaultmanager
{
    self.jsonManager = [AFHTTPSessionManager manager];
    self.jsonManager.requestSerializer = [AFJSONRequestSerializer serializer];
    self.jsonManager.responseSerializer = [AFJSONResponseSerializer serializer];
    self.jsonManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json", @"text/javascript",@"text/plain", nil];
    self.jsonManager.requestSerializer.timeoutInterval = 20;
}

- (AFHTTPSessionManager *)httpManager
{
    if (!_httpManager) {
        _httpManager = [[AFHTTPSessionManager alloc]init];
    }
    
    return _httpManager;
}

- (void)lucky_api_post_addActive:(id)param comletion:(LCResult)finished
{
    NSString *url = [NSString stringWithFormat:@"%@/prizeLevel/add.do",self.urlbase];
    
    [self.jsonManager POST:url parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (finished) {
            finished(responseObject,nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (finished) {
            finished(nil,error);
        }
    }];
}

- (void)lucky_api_post_deleteActive:(id)param comletion:(LCResult)finished
{
    NSString *url = [NSString stringWithFormat:@"%@/prizeLevel/delete.do",self.urlbase];
    
    [self.jsonManager POST:url parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (finished) {
            finished(responseObject,nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (finished) {
            finished(nil,error);
        }
    }];
}

- (void)lucky_api_post_queryActiveList:(id)param comletion:(LCResult)finished
{
    NSString *url = [NSString stringWithFormat:@"%@/prizeLevel/list.do",self.urlbase];
    [self.jsonManager POST:url parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (finished) {
            finished(responseObject,nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (finished) {
            finished(nil,error);
        }
    }];
}

- (void)lucky_api_post_memberlist:(id)param comletion:(LCResult)finished
{
    NSString *url = [NSString stringWithFormat:@"%@/employeeRegistration/page.do",self.urlbase];
    [self.jsonManager POST:url parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (finished) {
            finished(responseObject,nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (finished) {
            finished(nil,error);
        }
    }];
}

- (void)lucky_api_post_queryLucyResult:(id)param comletion:(LCResult)finished
{
    NSString *url = [NSString stringWithFormat:@"%@/employeeRegistration/generate.do",self.urlbase];
    [self.jsonManager POST:url parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (finished) {
            finished(responseObject,nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (finished) {
            finished(nil,error);
        }
    }];
}

- (void)lucky_api_post_queryResultList:(id)param comletion:(LCResult)finished
{
    NSString *url = [NSString stringWithFormat:@"%@/employeePrized/list.do",self.urlbase];
    [self.jsonManager POST:url parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (finished) {
            finished(responseObject,nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (finished) {
            finished(nil,error);
        }
    }];
}

- (void)lucky_api_post_export:(id)param comletion:(LCResult)finished
{
    NSString *url = [NSString stringWithFormat:@"%@/employeePrized/export.do",self.urlbase];
    self.httpManager.requestSerializer = [AFJSONRequestSerializer serializer];
    self.httpManager.responseSerializer = [AFHTTPResponseSerializer serializer];
    self.httpManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/octet-stream", nil];
    
    [self.httpManager POST:url parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (finished) {
            finished(responseObject,nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (finished) {
            finished(nil,error);
        }
    }];
}

- (void)lucky_api_post_finish_all:(id)param comletion:(LCResult)finished
{
    NSString *url = [NSString stringWithFormat:@"%@/employeeRegistration/batchGenerate.do",self.urlbase];
    [self.jsonManager POST:url parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (finished) {
            finished(responseObject,nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (finished) {
            finished(nil,error);
        }
    }];
}

- (void)lucky_api_post_send:(id)param comletion:(LCResult)finished
{
    NSString *url = [NSString stringWithFormat:@"%@/employeeRegistration/sendMsg.do",self.urlbase];
    [self.jsonManager POST:url parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (finished) {
            finished(responseObject,nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (finished) {
            finished(nil,error);
        }
    }];
}

@end
